package GUI.view;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;

/**
 * 
 * @author Wei WANG
 */

public class CombBoxHistory extends JPanel implements ItemListener {

	private static final long serialVersionUID = -4520357383286482026L;

	private JComboBox combo;
	private String itemSelected;

	private Object[] sList;
	private TreeConfAddPanelFormule tcf;

	public CombBoxHistory(Object[] sList, TreeConfAddPanelFormule tcf) {
		this.tcf = tcf;
		this.sList = sList;
		setBackground(Color.gray);
		this.setLayout(new BorderLayout());

		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
		this.add(topPanel, BorderLayout.CENTER);

		// Create a combobox
		combo = new JComboBox();
		combo.setBounds(20, 35, 260, 20);
		combo.addItem(" ");
		topPanel.add(combo, BorderLayout.CENTER);

		// Populate the combobox list
		for (int iCtr = 0; iCtr < sList.length; iCtr++)
			combo.addItem(sList[iCtr]);

		// Allow edits
		combo.setEditable(true);

		// Watch for changes
		combo.addItemListener(this);
	}

	public void desactive() {
		changeComboBoxToLabel("Desactive");
	}

	public String getItemSelected() {
		return itemSelected;
	}

	public void itemStateChanged(ItemEvent event) {
		if (event.getSource() == combo
				&& event.getStateChange() == ItemEvent.SELECTED) {
			this.itemSelected = (String) combo.getSelectedItem();
			System.out.println("Change:" + combo.getSelectedItem());
			if (combo.getSelectedItem().equals("execute")) {
				tcf.desactiveReceiverAndConversation();
			}
			for (int i = 0; i < sList.length; i++) {
				if (sList[i].equals(combo.getSelectedItem()))
					return;
			}
			combo.addItem(combo.getSelectedItem());
		}
	}

	public void setComboBoxItem(String str) {
		combo.setSelectedItem(str);
	}

	public void changeComboBoxToLabel(String label) {
		this.removeAll();
		JPanel topPanel = new JPanel();
		topPanel.setLayout(new BorderLayout());
		this.add(topPanel, BorderLayout.CENTER);

		// Create a combobox
		JLabel l = new JLabel(label);
		topPanel.add(l, BorderLayout.CENTER);
		topPanel.setBounds(20, 35, 260, 20);
		this.updateUI();
	}

	/*private static void createAndShowGUI() {
		String[] sList = {};
		String[] labelStrings = { "Street address: ", "City: " };
		JFrame frame = new JFrame("Demo");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// Add contents to the window.
		CombBoxHistory c = new CombBoxHistory(labelStrings, null);
		// c.desactive();
		frame.add(c);
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		javax.swing.SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				UIManager.put("swing.boldMetal", Boolean.FALSE);
				createAndShowGUI();
			}
		});
	}*/

}